//package com.wuhanins.insmytestdemo.ui.test;
//
//import android.annotation.TargetApi;
//import android.app.Notification;
//import android.app.NotificationChannel;
//import android.app.NotificationManager;
//import android.app.PendingIntent;
//import android.content.Context;
//import android.content.Intent;
//import android.os.Build;
//import android.os.Bundle;
//import android.text.TextUtils;
//import android.util.Log;
//import android.view.View;
//import android.widget.CheckBox;
//import android.widget.CompoundButton;
//import android.widget.Toast;
//
//import androidx.annotation.Nullable;
//import androidx.core.app.NotificationCompat;
//
//import com.amap.api.maps.CameraUpdateFactory;
//import com.amap.api.maps.model.MyLocationStyle;
//import com.amap.api.services.core.ServiceSettings;
//import com.amap.api.track.AMapTrackClient;
//import com.amap.api.track.ErrorCode;
//import com.amap.api.track.OnTrackLifecycleListener;
//import com.amap.api.track.TrackParam;
//import com.amap.api.track.query.model.AddTerminalRequest;
//import com.amap.api.track.query.model.AddTerminalResponse;
//import com.amap.api.track.query.model.AddTrackRequest;
//import com.amap.api.track.query.model.AddTrackResponse;
//import com.amap.api.track.query.model.QueryTerminalRequest;
//import com.amap.api.track.query.model.QueryTerminalResponse;
//import com.wuhanins.common.utils.GsonHelper;
//import com.wuhanins.common.utils.INSLog;
//import com.wuhanins.insmytestdemo.R;
//import com.wuhanins.insmytestdemo.base.BaseActivity;
//import com.wuhanins.insmytestdemo.databinding.ActivityElectronicFenceBinding;
//
///**
// * @author: Gab
// * @Project: fileService
// * @Pcakage: com.wuhanins.insmytestdemo.ui.test.ElectronicFenceActivity
// * @Date: 2023年08月25日 14:51
// * @Description: 电子围栏测试
// */
//public class ElectronicFenceActivity extends BaseActivity<ActivityElectronicFenceBinding> {
//
//    private static final String TAG = "TrackServiceActivity";
//    private static final String CHANNEL_ID_SERVICE_RUNNING = "CHANNEL_ID_SERVICE_RUNNING";
//
//    private AMapTrackClient aMapTrackClient;
//
//    private long terminalId;
//    private long trackId;
//    private boolean isServiceRunning;
//    private boolean isGatherRunning;
//
//    private boolean uploadToTrack = false;
//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(ActivityElectronicFenceBinding.class);
////        binding.activityTrackServiceMap.onCreate(savedInstanceState);
//    }
//
//    @Override
//    protected void iView() {
//
//    }
//
//    @Override
//    protected void iData() {
//        ServiceSettings.updatePrivacyShow(this, true, true);
//        ServiceSettings.updatePrivacyAgree(this, true);
//        // 不要使用Activity作为Context传入
//        try {
//            aMapTrackClient = new AMapTrackClient(getApplicationContext());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        aMapTrackClient.setInterval(5, 30);
////        binding.activityTrackServiceMap.getMap().moveCamera(CameraUpdateFactory.zoomTo(14));
////        // 启用地图内置定位
////        binding.activityTrackServiceMap.getMap().setMyLocationEnabled(true);
////        binding.activityTrackServiceMap.getMap().setMyLocationStyle(
////                new MyLocationStyle()
////                        .interval(2000)
////                        .myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)
////        );
//        updateBtnStatus();
//        // 服务启停
//
//        binding.activityTrackServiceStartTrack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (isServiceRunning) {
//                    aMapTrackClient.stopTrack(new TrackParam(Constants.SERVICE_ID, terminalId), onTrackListener);
//                } else {
//                    startTrack();
//                }
//            }
//        });
//        // 采集启停
//        binding.activityTrackServiceStartGather.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (isGatherRunning) {
//                    aMapTrackClient.stopGather(onTrackListener);
//                } else {
//                    aMapTrackClient.setTrackId(trackId);
//                    aMapTrackClient.startGather(onTrackListener);
//                }
//            }
//        });
//
//        ((CheckBox) findViewById(R.id.activity_track_service_upload_to_track)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                uploadToTrack = isChecked;
//            }
//        });
//    }
//
//    private OnTrackLifecycleListener onTrackListener = new SimpleOnTrackLifecycleListener() {
//        @Override
//        public void onBindServiceCallback(int status, String msg) {
//            Log.w(TAG, "onBindServiceCallback, status: " + status + ", msg: " + msg);
//        }
//
//        @Override
//        public void onStartTrackCallback(int status, String msg) {
//            if (status == ErrorCode.TrackListen.START_TRACK_SUCEE || status == ErrorCode.TrackListen.START_TRACK_SUCEE_NO_NETWORK) {
//                // 成功启动
//                Toast.makeText(ElectronicFenceActivity.this, "启动服务成功", Toast.LENGTH_SHORT).show();
//                isServiceRunning = true;
//                updateBtnStatus();
//            } else if (status == ErrorCode.TrackListen.START_TRACK_ALREADY_STARTED) {
//                // 已经启动
//                Toast.makeText(ElectronicFenceActivity.this, "服务已经启动", Toast.LENGTH_SHORT).show();
//                isServiceRunning = true;
//                updateBtnStatus();
//            } else {
//                Log.w(TAG, "error onStartTrackCallback, status: " + status + ", msg: " + msg);
//                Toast.makeText(ElectronicFenceActivity.this,
//                        "error onStartTrackCallback, status: " + status + ", msg: " + msg,
//                        Toast.LENGTH_LONG).show();
//            }
//        }
//
//        @Override
//        public void onStopTrackCallback(int status, String msg) {
//            if (status == ErrorCode.TrackListen.STOP_TRACK_SUCCE) {
//                // 成功停止
//                Toast.makeText(ElectronicFenceActivity.this, "停止服务成功", Toast.LENGTH_SHORT).show();
//                isServiceRunning = false;
//                isGatherRunning = false;
//                updateBtnStatus();
//            } else {
//                Log.w(TAG, "error onStopTrackCallback, status: " + status + ", msg: " + msg);
//                Toast.makeText(ElectronicFenceActivity.this,
//                        "error onStopTrackCallback, status: " + status + ", msg: " + msg,
//                        Toast.LENGTH_LONG).show();
//
//            }
//        }
//
//        @Override
//        public void onStartGatherCallback(int status, String msg) {
//            if (status == ErrorCode.TrackListen.START_GATHER_SUCEE) {
//                Toast.makeText(ElectronicFenceActivity.this, "定位采集开启成功", Toast.LENGTH_SHORT).show();
//                isGatherRunning = true;
//                updateBtnStatus();
//            } else if (status == ErrorCode.TrackListen.START_GATHER_ALREADY_STARTED) {
//                Toast.makeText(ElectronicFenceActivity.this, "定位采集已经开启", Toast.LENGTH_SHORT).show();
//                isGatherRunning = true;
//                updateBtnStatus();
//            } else {
//                Log.w(TAG, "error onStartGatherCallback, status: " + status + ", msg: " + msg);
//                Toast.makeText(ElectronicFenceActivity.this,
//                        "error onStartGatherCallback, status: " + status + ", msg: " + msg,
//                        Toast.LENGTH_LONG).show();
//            }
//        }
//
//        @Override
//        public void onStopGatherCallback(int status, String msg) {
//            if (status == ErrorCode.TrackListen.STOP_GATHER_SUCCE) {
//                Toast.makeText(ElectronicFenceActivity.this, "定位采集停止成功", Toast.LENGTH_SHORT).show();
//                isGatherRunning = false;
//                updateBtnStatus();
//            } else {
//                Log.w(TAG, "error onStopGatherCallback, status: " + status + ", msg: " + msg);
//                Toast.makeText(ElectronicFenceActivity.this,
//                        "error onStopGatherCallback, status: " + status + ", msg: " + msg,
//                        Toast.LENGTH_LONG).show();
//            }
//        }
//    };
//
//    private void updateBtnStatus() {
//        binding.activityTrackServiceStartTrack.setText(isServiceRunning ? "停止服务" : "启动服务");
//        binding.activityTrackServiceStartTrack.setTextColor(isServiceRunning ? 0xFFFFFFFF : 0xFF000000);
//        binding.activityTrackServiceStartTrack.setBackgroundResource(isServiceRunning ? R.drawable.round_corner_btn_bg_active : R.drawable.round_corner_btn_bg);
//        binding.activityTrackServiceStartGather.setText(isGatherRunning ? "停止采集" : "开始采集");
//        binding.activityTrackServiceStartGather.setTextColor(isGatherRunning ? 0xFFFFFFFF : 0xFF000000);
//        binding.activityTrackServiceStartGather.setBackgroundResource(isGatherRunning ? R.drawable.round_corner_btn_bg_active : R.drawable.round_corner_btn_bg);
//    }
//
//    private void startTrack() {
//        // 先根据Terminal名称查询Terminal ID，如果Terminal还不存在，就尝试创建，拿到Terminal ID后，
//        // 用Terminal ID开启轨迹服务
//        aMapTrackClient.queryTerminal(new QueryTerminalRequest(Constants.SERVICE_ID, Constants.TERMINAL_NAME), new SimpleOnTrackListener() {
//            @Override
//            public void onQueryTerminalCallback(QueryTerminalResponse queryTerminalResponse) {
//                if (queryTerminalResponse.isSuccess()) {
//                    if (queryTerminalResponse.isTerminalExist()) {
//                        // 当前终端已经创建过，直接使用查询到的terminal id
//                        terminalId = queryTerminalResponse.getTid();
//                        if (uploadToTrack) {
//                            aMapTrackClient.addTrack(new AddTrackRequest(Constants.SERVICE_ID, terminalId), new SimpleOnTrackListener() {
//                                @Override
//                                public void onAddTrackCallback(AddTrackResponse addTrackResponse) {
//                                    if (addTrackResponse.isSuccess()) {
//                                        // trackId需要在启动服务后设置才能生效，因此这里不设置，而是在startGather之前设置了track id
//                                        trackId = addTrackResponse.getTrid();
//                                        TrackParam trackParam = new TrackParam(Constants.SERVICE_ID, terminalId);
//                                        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
////                                            trackParam.setNotification(createNotification());
//                                            makeNotification(ElectronicFenceActivity.this, trackParam, 10002);
//                                        }
//                                        aMapTrackClient.startTrack(trackParam, onTrackListener);
//                                    } else {
//                                        Toast.makeText(ElectronicFenceActivity.this, "网络请求失败，" + addTrackResponse.getErrorMsg(), Toast.LENGTH_SHORT).show();
//                                    }
//                                }
//                            });
//                        } else {
//                            // 不指定track id，上报的轨迹点是该终端的散点轨迹
//                            TrackParam trackParam = new TrackParam(Constants.SERVICE_ID, terminalId);
//                            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
////                                trackParam.setNotification(createNotification());
//                                makeNotification(ElectronicFenceActivity.this, trackParam, 10002);
//                            }
//                            aMapTrackClient.startTrack(trackParam, onTrackListener);
//                        }
//                    } else {
//                        // 当前终端是新终端，还未创建过，创建该终端并使用新生成的terminal id
//                        aMapTrackClient.addTerminal(new AddTerminalRequest(Constants.TERMINAL_NAME, Constants.SERVICE_ID), new SimpleOnTrackListener() {
//                            @Override
//                            public void onCreateTerminalCallback(AddTerminalResponse addTerminalResponse) {
//                                if (addTerminalResponse.isSuccess()) {
//                                    terminalId = addTerminalResponse.getTid();
//                                    TrackParam trackParam = new TrackParam(Constants.SERVICE_ID, terminalId);
//                                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
////                                        trackParam.setNotification(createNotification());
//                                        makeNotification(ElectronicFenceActivity.this, trackParam, 10002);
//                                    }
//                                    aMapTrackClient.startTrack(trackParam, onTrackListener);
//                                } else {
//                                    Toast.makeText(ElectronicFenceActivity.this, "网络请求失败，" + addTerminalResponse.getErrorMsg(), Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        });
//                    }
//                } else {
//                    Toast.makeText(ElectronicFenceActivity.this, "网络请求失败，" + queryTerminalResponse.getErrorMsg(), Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//    }
//
//    /**
//     * 在8.0以上手机，如果app切到后台，系统会限制定位相关接口调用频率
//     * 可以在启动轨迹上报服务时提供一个通知，这样Service启动时会使用该通知成为前台Service，可以避免此限制
//     */
//    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
//    private Notification createNotification() {
//        Notification.Builder builder;
//        NotificationManager nm;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//            NotificationChannel channel = new NotificationChannel(CHANNEL_ID_SERVICE_RUNNING, "app service", NotificationManager.IMPORTANCE_LOW);
//            nm.createNotificationChannel(channel);
//            builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID_SERVICE_RUNNING);
//        } else {
//            builder = new Notification.Builder(getApplicationContext());
//        }
//        Intent nfIntent = new Intent(ElectronicFenceActivity.this, ElectronicFenceActivity.class);
//        nfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        builder.setContentIntent(PendingIntent.getActivity(ElectronicFenceActivity.this, 0, nfIntent, 0))
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentTitle("猎鹰sdk运行中")
//                .setContentText("猎鹰sdk运行中");
//        return builder.build();
//    }
//
//    public void makeNotification(Context context, TrackParam trackParam, int id) {
//        NotificationManager notificationManager = (NotificationManager) context.getSystemService(
//                Context.NOTIFICATION_SERVICE);
//        Intent intent = new Intent(context, ElectronicFenceActivity.class);
//        PendingIntent contentIntent = PendingIntent.getActivity(
//                context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_SERVICE_RUNNING, "app service",
//                    NotificationManager.IMPORTANCE_HIGH);
//            notificationManager.createNotificationChannel(notificationChannel);
//        }
//        Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID_SERVICE_RUNNING)
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentTitle("猎鹰sdk运行中")
//                .setContentText("猎鹰sdk运行中")
//                .setDefaults(Notification.DEFAULT_ALL)
//                .setContentIntent(contentIntent)
//                .setAutoCancel(true)//设置点击之后notification消失
//                .build();
//        notificationManager.notify(id, notification);
//        trackParam.setNotification(notification);
//    }
//
//
//    @Override
//    protected void onPause() {
//        super.onPause();
////        binding.activityTrackServiceMap.onPause();
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
////        binding.activityTrackServiceMap.onResume();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
////        binding.activityTrackServiceMap.onDestroy();
//        if (isServiceRunning) {
//            aMapTrackClient.stopTrack(new TrackParam(Constants.SERVICE_ID, terminalId), new SimpleOnTrackLifecycleListener());
//        }
//    }
//
//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
////        binding.activityTrackServiceMap.onSaveInstanceState(outState);
//    }
//}
